package com.wm.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wm.common.utils.StringUtils;
import com.wm.system.domain.SysMission;

import com.wm.system.filter.SysMissionSearchContext;
import com.wm.system.mapper.SysMissionMapper;
import com.wm.system.service.ISysMissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 待办任务服务类
 */
@Slf4j
@Service
public class SysMissionServiceImpl extends ServiceImpl<SysMissionMapper, SysMission> implements ISysMissionService {

    @Autowired
    private SysMesgSendService sysMesgSendService;

    @Override
    public void createMission(String missionName, String missionType, List<Long> receiverId, Long businessId, String node, String isSingle) {

        if (receiverId != null) {
            List<SysMission> sysMissions = new ArrayList<>();
            receiverId.forEach(r -> {

                SysMission mission = new SysMission();
                mission.setMissionName(missionName);
                mission.setMissionType(missionType);
                mission.setReceiverId(r);
                mission.setBusinessId(businessId);
                mission.setNode(node);
                mission.setStatus(SysMission.NOT_COMPLETED);
                mission.setIsSingle(isSingle);

                sysMissions.add(mission);
            });
            saveBatch(sysMissions);

            //发送待办提醒
            sysMesgSendService.sendSocketMsg(receiverId);
        }
    }


    @Override
    public void createMission(String missionName, String missionType, Long receiverId, Long businessId, String node) {
        if (receiverId != null) {
            List<Long> uids = new ArrayList<>();
            uids.add(receiverId);
            createMission(missionName,missionType, uids, businessId, node, SysMission.IS_SINGLE);
        }
    }

    @Override
    public void completMission(String missionType, Long uid, String businessId, String node) {
        if (StringUtils.isNotEmpty(missionType) && uid != null && StringUtils.isNotEmpty(businessId)) {

            UpdateWrapper<SysMission> wrapper = new UpdateWrapper<>();
            wrapper.lambda().eq(SysMission::getMissionType, missionType)
                    .eq(SysMission::getReceiverId, uid)
                    .eq(SysMission::getBusinessId, businessId)
                    .set(SysMission::getStatus, SysMission.COMPLETED);

            if (StringUtils.isNotEmpty(node)) {
                wrapper.lambda().eq(SysMission::getNode, node);
            }
            update(wrapper);
        }
    }

    @Override
    public Page getPage(SysMissionSearchContext context) {
        return page(context.getPages(), context.getSelectCond());
    }
}
